﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	internal class ClassMultiPipeXMLInfo
	{
		public static void InitializeFilePath(string path, string rootNode)
		{
			if (!File.Exists(path))
			{
				if (File.Exists(Path.Combine(Product.DataLocation, "HYDrawParallelPipeInfo.xml")))
				{
					File.Copy(Path.Combine(Product.DataLocation, "HYDrawParallelPipeInfo.xml"), path);
					return;
				}
				XmlDocument xmlDocument = new XmlDocument();
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement xmlElement = xmlDocument.CreateElement(rootNode);
				xmlDocument.AppendChild(xmlElement);
				XmlNode xmlNode = xmlDocument.CreateElement("nodeAlongWall");
				XmlNode xmlNode2 = xmlDocument.CreateElement("distanceType");
				xmlNode2.InnerText = "0";
				xmlNode.AppendChild(xmlNode2);
				XmlNode xmlNode3 = xmlDocument.CreateElement("alongWallDis");
				xmlNode3.InnerText = "150";
				xmlNode.AppendChild(xmlNode3);
				xmlElement.AppendChild(xmlNode);
				XmlNode xmlNode4 = xmlDocument.CreateElement("nodePipeDeriDraw");
				XmlNode xmlNode5 = xmlDocument.CreateElement("offType");
				xmlNode5.InnerText = "0";
				xmlNode4.AppendChild(xmlNode5);
				XmlNode xmlNode6 = xmlDocument.CreateElement("offDis");
				xmlNode6.InnerText = "150";
				xmlNode4.AppendChild(xmlNode6);
				xmlElement.AppendChild(xmlNode4);
				XmlNode newChild2 = xmlDocument.CreateElement("GPSPipe");
				xmlElement.AppendChild(newChild2);
				XmlNode newChild3 = xmlDocument.CreateElement("AirConditioningWaterPipe");
				xmlElement.AppendChild(newChild3);
				XmlNode newChild4 = xmlDocument.CreateElement("HeatingPipe");
				xmlElement.AppendChild(newChild4);
				xmlDocument.Save(path);
			}
		}

		public static string GetSelectSingleNodeValue(string path, string selectSingleNodeName)
		{
			string result = "";
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode(selectSingleNodeName);
			if (xmlNode != null)
			{
				result = xmlNode.InnerText;
			}
			return result;
		}

		public static void UpdateSelectSingleNodeValue(string path, string selectSingleNodeName, string selectSingleNodeValue)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlElement documentElement = xmlDocument.DocumentElement;
			XmlNode xmlNode = documentElement.SelectSingleNode(selectSingleNodeName);
			if (xmlNode == null)
			{
				string[] array = selectSingleNodeName.Split(new char[]
				{
					'/'
				});
				XMLOperating.EditNodeText(path, array[0], array[1], selectSingleNodeValue);
				xmlNode = documentElement.SelectSingleNode(selectSingleNodeName);
			}
			xmlNode.InnerText = selectSingleNodeValue;
			xmlDocument.Save(path);
		}

		public static void WritePipeStruct(string path, string selectSingleNodeName, List<DrawPipeStruct> listStruct)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNode xmlNode = xmlDocument.DocumentElement.SelectSingleNode(selectSingleNodeName);
			xmlNode.RemoveAll();
			for (int i = 0; i < listStruct.Count; i++)
			{
				DrawPipeStruct drawPipeStruct = listStruct.ElementAt(i);
				XmlNode xmlNode2 = xmlDocument.CreateElement("listPipeStructAdd" + i);
				(xmlNode2 as XmlElement).SetAttribute("name", drawPipeStruct.pipingSystem.Name);
				(xmlNode2 as XmlElement).SetAttribute("pipeType", drawPipeStruct.pipeType.Name);
				(xmlNode2 as XmlElement).SetAttribute("pipeDiameter", drawPipeStruct.pipeDiameter.ToString());
				(xmlNode2 as XmlElement).SetAttribute("levelOffSer", drawPipeStruct.levelOffSet.ToString());
				(xmlNode2 as XmlElement).SetAttribute("offSet", drawPipeStruct.offSet.ToString());
				xmlNode.AppendChild(xmlNode2);
			}
			xmlDocument.Save(path);
		}

		public static List<ReadPipeStruct> RaedInfoFromXML(string path, string selectSingleNodeName)
		{
			List<ReadPipeStruct> list = new List<ReadPipeStruct>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNodeList childNodes = xmlDocument.DocumentElement.SelectSingleNode(selectSingleNodeName).ChildNodes;
			if (childNodes.Count > 0)
			{
				foreach (object obj in childNodes)
				{
					XmlElement xmlElement = (XmlElement)((XmlNode)obj);
					list.Add(new ReadPipeStruct
					{
						pipingSysName = xmlElement.Attributes["name"].Value,
						pipeTypeName = xmlElement.Attributes["pipeType"].Value,
						pipeDiameter = xmlElement.Attributes["pipeDiameter"].Value,
						levelOffSet = xmlElement.Attributes["levelOffSer"].Value,
						offSet = xmlElement.Attributes["offSet"].Value
					});
				}
			}
			return list;
		}
	}
}
